package com.lesama.calendar;



import java.util.Calendar;
import java.util.Date;

public class CalendarDemo {
    /*
     * Calendar是一个抽象类，不能创建对象，需要调用里面的静态方法getInstance获取子类对象
     *       public static Calendar getInstance(): 获取当前日期的日历对象
     *
     * 常用方法：
     *       public int get(int field): 获取日历中某个字段的信息
     *       public void set(int field, int value): 修改日历中某个字段的信息
     *       public void add(int field, int value): 为某个字段增加/减少指定的值
     * */
    public static void main(String[] args) {
        getMethod();
        System.out.println("--------------------------------------------");
        setMethod();
        System.out.println("--------------------------------------------");
        addMethod();
    }

    private static void addMethod() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 2);
        System.out.println(calendar.get(Calendar.YEAR));
        calendar.add(Calendar.MONTH, -2);
        System.out.println(calendar.get(Calendar.MONTH));
    }

    private static void setMethod() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2026); // 设置年份
        System.out.println(calendar.get(Calendar.YEAR));
        calendar.set(2027, Calendar.AUGUST, 19);
        System.out.println(calendar.get(Calendar.YEAR));
    }

    private static void getMethod() {
        Calendar calendar = Calendar.getInstance(); // 获取当前日期的日历对象
        Date time = calendar.getTime();
        System.out.println(time.getTime());
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH)); // 月份从0开始
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        char[] weeks = {'0', '日', '一', '二', '三', '四', '五', '六'};
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println(weeks[weekIndex]);
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 星期从日开始
    }
}